﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.ILAst;
using Mono.Cecil;

namespace CSLua
{
    // Token: 0x0200001C RID: 28
    public static class BuilderUtils
    {
        public static string[] CODS =
        {
            "0","1","2","3","4","5","6","7","8","9",
            "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
            "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
            "_0","_1"
        };
        // Token: 0x060000F9 RID: 249 RVA: 0x00005B00 File Offset: 0x00003D00
        public static string luaLite(this string str)
        {
            str = str.Replace("\\", "\\\\");
            str = str.Replace("\n", "\\n");
            str = str.Replace("\r", "\\r");
            str = str.Replace("\t", "\\t");
            str = str.Replace("'", "\\'");
            str = str.Replace("\0", "\\0");
            return "'" + str + "'";
        }

        // Token: 0x060000FA RID: 250 RVA: 0x00005B8C File Offset: 0x00003D8C
        public static string ToString(ILNode block)
        {
            StringWriter w = new StringWriter();
            block.WriteTo(new PlainTextOutput(w));
            return w.ToString().Replace("\r\n", "\r\n;");
        }

        // Token: 0x060000FB RID: 251 RVA: 0x00005BC0 File Offset: 0x00003DC0
        public static void DebugLine(string line)
        {
            Console.WriteLine(line);
        }

        // Token: 0x060000FC RID: 252 RVA: 0x00005BC8 File Offset: 0x00003DC8
        public static void ErrorLine(string line)
        {
            Console.Error.WriteLine(line);
        }

        private static readonly StringBuilder _idBuilder = new StringBuilder();

        private static void To64(StringBuilder sb, char c)
        {
            while (c > 0)
            {
                var i = c & 0x3f;
                sb.Append(CODS[i]);
                c >>= 6;
            }
        }
        // 格式化标识符
        public static string toIdentifier(string name)
        {
            _idBuilder.Clear();
            var isUnicode = false;
            for (var i = 0; i < name.Length; i++)
            {
                var c = name[i];
                if (c == '.' || c == '<' || c == '>')
                    c = '_';
                else if (!(c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_'))
                {
                    if (!isUnicode)
                        _idBuilder.Append("_u");
                    isUnicode = true;
                    To64(_idBuilder,c);
                    //_idBuilder.Append($"{((int)c):X2}");
                    continue;
                }
                if (isUnicode)
                    _idBuilder.Append("u_");
                isUnicode = false;
                _idBuilder.Append(c);
            }
            if (isUnicode)
                _idBuilder.Append("u_");
            return _idBuilder.ToString();
        }


        // Token: 0x060000FE RID: 254 RVA: 0x00005BE8 File Offset: 0x00003DE8
        public static string GetTypeFullname(TypeDefinition def, bool withNameSpace = true)
        {
            def.luaIdentifier();
            TypeDefinition decFileType;
            string nestPath;
            string fullName = BuilderUtils.GetTypeNestPath(def, out decFileType, out nestPath);
            if (withNameSpace && !string.IsNullOrEmpty(decFileType.Namespace))
            {
                fullName = decFileType.Namespace + "." + fullName;
            }
            return fullName;
        }

        // Token: 0x060000FF RID: 255 RVA: 0x00005C2C File Offset: 0x00003E2C
        public static string GetTypeNestPath(TypeDefinition def, out TypeDefinition decFileType, out string nestPath)
        {
            string fullPath = def.luaIdentifier();
            nestPath = "";
            while (def.DeclaringType != null)
            {
                nestPath = fullPath;
                fullPath = def.DeclaringType.luaIdentifier() + "." + fullPath;
                def = def.DeclaringType;
            }
            decFileType = def;
            return fullPath;
        }
    }
}
